<?php
class CareersController extends AppController {

	var $name = 'Careers';
	
	public function beforeFilter() {
		$this->Auth->allow('ajaxCareerOption');
	}

	function admin_index() {
		$this->Career->recursive = 0;
		$this->set('careers', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid career', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('career', $this->Career->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Career->create();
			if ($this->Career->save($this->data)) {
				$this->Session->setFlash(__('The career has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The career could not be saved. Please, try again.', true));
			}
		}
		$cities = $this->Career->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid career', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Career->save($this->data)) {
				$this->Session->setFlash(__('The career has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The career could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Career->read(null, $id);
		}
		$cities = $this->Career->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for career', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Career->delete($id)) {
			$this->Session->setFlash(__('Career deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Career was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function ajaxCareerOption($city_id = 1)
	{
		//echo $city_id;
		$careers = $this->Career->find("all", array("order"=>"Career.name"));
		//var_dump($careers[0]);
		//var_dump();
		//echo count($districts);
		$this->set(compact('careers'));
		$this->layout = null;
	}
}
